public class YesterdayDateCalculator {

    // 判断是否为闰年
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    // 计算前一天日期
    public static String YesterDate(int year, int month, int day) {
        // 检查年份范围
        if (year < 1900 || year > 2050) {
            throw new IllegalArgumentException("年份必须在 1900 到 2050 之间");
        }
        // 检查月份范围
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("月份必须在 1 到 12 之间");
        }
        int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeapYear(year)) {
            daysInMonth[1] = 29;
        }
        // 检查日期范围
        if (day < 1 || day > daysInMonth[month - 1]) {
            throw new IllegalArgumentException("输入的日期不合法");
        }

        if (day > 1) {
            return year + "-" + month + "-" + (day - 1);
        } else if (month > 1) {
            int prevMonth = month - 1;
            return year + "-" + prevMonth + "-" + daysInMonth[prevMonth - 1];
        } else {
            return (year - 1) + "-12-31";
        }
    }
}
